package View;

import java.util.ArrayList;
import java.util.List;

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Group;
import javafx.scene.control.Label;
import javafx.scene.control.MultipleSelectionModel;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.shape.Rectangle;
import CustomClasses.CustomConnection;

public class Map {

	//Elements on the Map - like Nodes
	public static Group elements = new Group();
	public static Group connections = new Group();
	public static Group connectionLabel = new Group();
	public static Group nodeLabel = new Group();
	public static Path editConnection = new Path();
	public static BooleanProperty nodeLabelVisible = new SimpleBooleanProperty(false);

	//STARTKNOTEN startProperty.getValue() -> String
	public static StringProperty startProperty = new SimpleStringProperty("");
	//ENDKNOTEN
	public static StringProperty endProperty = new SimpleStringProperty("");

	public static StringProperty selectConnection = new SimpleStringProperty("");
	public static List<CustomConnection> customValue = new ArrayList<CustomConnection>();

	public static Group map() {
		initPath();
		Group group = new Group();
		group.maxWidth(650);
		group.maxHeight(600);
		Rectangle map = new Rectangle();
		map.setWidth(650);
		map.setHeight(600);
		map.setFill(Color.WHITESMOKE);
		map.setStroke(Color.BLACK);
		map.setStrokeWidth(0.75);
		map.setArcHeight(5);
		map.setArcWidth(5);

		ImageView iv = new ImageView();
		iv.setFitWidth(40);
		iv.setScaleX(-1);
		iv.setImage(new Image("C:\\Users\\zonx\\Pictures\\aladin1.gif"));

		group.getChildren().addAll(map, connections, elements, editConnection, connectionLabel, nodeLabel);

		return group;
	}

	public static void initPath() {
		editConnection.setStroke(Color.BLACK);
		editConnection.setStrokeWidth(1);
		editConnection.getElements().add(new MoveTo(0, 0));
		editConnection.getElements().add(new LineTo(0, 0));
	}

	public static void calcConnections() {
		connectionLabel.getChildren().clear();
		for (int i = 0; i < customValue.size(); i++) {
			final Label label = customValue.get(i).getLabel();
			if (!customValue.get(i).getDisable()) {
				Path path = (Path) connections.getChildren().get(i);
				path.setVisible(true);
				MoveTo move = (MoveTo) path.getElements().get(0);
				LineTo line = (LineTo) path.getElements().get(1);

				Double root;
				root = Math.sqrt(Math.pow(Math.abs(move.getX() - line.getX()), 2) + Math.pow(Math.abs(move.getY() - line.getY()), 2));
				label.setVisible(true);
				label.setLayoutX(((move.getX() + line.getX()) / 2) - label.getBoundsInLocal().getWidth() / 2);
				label.setLayoutY(((move.getY() + line.getY()) / 2) - label.getBoundsInLocal().getHeight() / 2);

				selectConnection.addListener(new ChangeListener<String>() {
					@Override
					public void changed(ObservableValue<? extends String> arg0, String arg1, String arg2) {
						if (!label.getId().equals(selectConnection.getValue())) {
							label.setTextFill(Color.WHITE);
						} else {
							label.setTextFill(Color.ORANGE);
							MultipleSelectionModel<String> ms = SideBar.listView.getSelectionModel();
							ms.select(Integer.valueOf(label.getId().substring(5)));
							SideBar.listView.selectionModelProperty().set(ms);
							SideBar.listView.scrollTo(ms.getSelectedIndex());
						}
					}
				});
				if (customValue.get(i).getCustomDistance()) {
					label.setText(Integer.toString(root.intValue()));
					customValue.get(i).setDistance(root.intValue());
				} else {
					label.setText(String.valueOf(customValue.get(i).getDistance()));
				}
				customValue.get(i).setLabel(label);
			}
			connectionLabel.getChildren().add(label);
		}
	}

	public static Label generateNodeLabel(Circle node) {
		Label nodeLabel = new Label();
		nodeLabel.setStyle("-fx-font: bold 15pt \"Helvetica\";");
		nodeLabel.setText(node.getId().substring(4));
		nodeLabel.setId("nodeLabel" + node.getId().substring(4));
		nodeLabel.setTextFill(Color.WHITE);
		//nodeLabel.visibleProperty().bind(Map.nodeLabelVisible);
		nodeLabel.mouseTransparentProperty().setValue(true);
		nodeLabel.layoutXProperty().bind(node.centerXProperty().subtract(5));
		nodeLabel.layoutYProperty().bind(node.centerYProperty().subtract(10));
		return nodeLabel;
	}
}
